package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import Conexao.ConnectionManager;
import Modelo.Aluno;
import Modelo.AlunoSI;

public class AlunoSiDaoImp implements AlunoSiDao  {
	
	private final String INSERT = "insert into alunosSi (nome, curso, data) values (?,?,?)";
	private final String UPDATE = "update alunosSi set nome = ?, curso = ?, data= ? where idAlunoSi = ?";
	private final String DELETE = "delete from alunosSi where idAlunoSi = ?";
	private final String ALL = "select * from alunosSi";
	 private final String ID = "select * from alunosSi where idAlunoSi = ?";

	private static Connection connection;
	
	public AlunoSiDaoImp() {
		openConnection();
		
	}
	
	public AlunoSI salvar(AlunoSI e) {
		try {
			PreparedStatement ps = null;

			ps = ConnectionManager.getInstance().getConnection()
					.prepareStatement(INSERT);

			ps.setObject(1, e.getNome());
			ps.setObject(2, e.getCurso());
			ps.setObject(3, e.getData());
			
			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(AlunoSiDaoImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
		return e;
	}
	
	public void alterar(AlunoSI e) {
		PreparedStatement ps = null;
		try {
			ps = connection.prepareStatement(UPDATE);

			ps.setObject(1, e.getNome());
			ps.setObject(2, e.getCurso());
			ps.setObject(3, e.getData());
			ps.setObject(4, e.getIdAlunoSi());

			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(AlunoSiDaoImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
	}
	
	public void deletar(AlunoSI e) {
		try {
			PreparedStatement ps = null;
			ps = connection.prepareStatement(DELETE);

			ps.setObject(1, e.getIdAlunoSi());

			ps.executeUpdate();

		} catch (SQLException ex) {
			Logger.getLogger(AlunoSiDaoImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
	}

	public List<AlunoSI> findAll() {
		List<AlunoSI> alunos = new ArrayList<AlunoSI>();

		try {
			PreparedStatement ps = null;
			ResultSet rs = null;

			ps = connection.prepareStatement(ALL);

			rs = ps.executeQuery();

			while (rs.next()) {
				AlunoSI aluno = new AlunoSI();
				aluno.setIdAlunoSi(rs.getInt("idAlunoSi"));
				aluno.setNome(rs.getString("nome"));
				aluno.setCurso(rs.getString("curso"));
				aluno.setData(rs.getDate("data"));
				
				alunos.add(aluno);
			}

		} catch (SQLException ex) {
			Logger.getLogger(AlunoSiDaoImp.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			this.closeConnection();
		}
		return alunos;
	}
	
	public boolean isConnectionClose() {
		try {
			return connection.isClosed();
		} catch (SQLException ex) {
			return false;
		}
	}

	public void openConnection() {
		if (connection == null) {
			connection = ConnectionManager.getInstance().getConnection();
		}

		if (isConnectionClose()) {
			connection = ConnectionManager.getInstance().getConnection();
		}
	}


	public void closeConnection() {
		if (connection != null && !this.isConnectionClose()) {
			ConnectionManager.getInstance().closeConnection(connection);
		}

	}

	@Override
	public AlunoSI BuscarPorId(int idAlunoSi) {
		List<AlunoSI> alunos = new ArrayList<AlunoSI>();

		try {
		PreparedStatement ps = null;
		ResultSet rs = null;

		ps = connection.prepareStatement(ID);
		ps.setObject(1, idAlunoSi);

		rs = ps.executeQuery();

		while (rs.next()) {
			AlunoSI aluno = new AlunoSI();
			aluno.setIdAlunoSi(rs.getInt("idAlunoSi"));
			aluno.setNome(rs.getString("nome"));
			aluno.setCurso(rs.getString("curso"));
			aluno.setData(rs.getDate("data"));
			
			alunos.add(aluno);
		}

		} catch (SQLException ex) {
		Logger.getLogger(AlunoSiDaoImp.class.getName()).log(Level.SEVERE,
		null, ex);
		} finally {
		this.closeConnection();
		}
		if (alunos.isEmpty()) {
		return null;
		} else {
		return alunos.get(0);
		}
	}

}
